﻿using UnityEngine;
using System.Collections;

public class GameController : MonoBehaviour {
	public GameObject pot1;
	public GameObject pot2;

	public GameObject cat;

	//
	public GameObject startScreen;
	public GameObject victory;
	public GameObject failed;
	public GameObject replay;

	public int columnNum = 9;
	public int rowNum = 9;

	private ArrayList potArray;
	private ArrayList pot2Array;
	private bool isStarted = false;
	private bool gameOver = false;

	// Use this for initialization
	void Start () {
		this.initAllPot();
		//MoveCat(4,4);

		//开始界面
		cat.SetActive(false);
		startScreen.SetActive(true);
		victory.SetActive(false);
		failed.SetActive(false);
		replay.SetActive(false);

	}

	void initAllPot(){
		pot2Array = new ArrayList();
		potArray = new ArrayList();
		
		for(int rowIndex=0; rowIndex < rowNum; rowIndex++){
			ArrayList tmp = new ArrayList();
			for(int columnIndex=0; columnIndex< columnNum; columnIndex++){
				/**
				GameObject obj = Instantiate(pot1) as GameObject;
				Item item = obj.GetComponent<Item>();
				item.game = this;
				item.transform.parent = this.transform;
				item.UpdatePos(rowIndex, columnIndex);

				**/
				Item item = createPot(pot1, rowIndex, columnIndex);
				tmp.Add(item);
			}
			potArray.Add(tmp);
		}
	}

	Item GetPot(int row, int column){
		if(column <0){
			return null;
		}
		if(column >= columnNum){
			return null;
		}
		if(row <0){
			return null;
		}
		if(row >=rowNum){
			return null;
		}

		ArrayList tmp = potArray[row] as ArrayList;
		Item item =  tmp[column] as Item;

		return item;
	}
	void MoveCat(int row, int column){
		Item item = cat.GetComponent<Item>();
		item.UpdatePos(row, column);
	}

	public void Select(Item item){

		if(isStarted == false || gameOver) return;
		//MoveCat(rowIndex, columnIndex);
		if(item.moveable){
			item.moveable = false;
			Item pot2Item = createPot(pot2, item.rowIndex, item.columnIndex);
			pot2Array.Add(pot2Item);
			ArrayList steps = FindSteps();
			Debug.Log(steps.Count);

			if(steps.Count >0){
				int index = Random.Range(0, steps.Count);
				Vector2  v= (Vector2)steps[index];
				MoveCat((int)v.y, (int)v.x);

				if(Escaped()){
					Debug.Log("Escaped....");

					gameOver = true;
					failed.SetActive(true);
					replay.SetActive(true);
				}
			}else{
				Debug.Log("Victory....");

				gameOver = true;
				victory.SetActive(false);
				replay.SetActive(true);
			}
		}
	}
	bool Escaped(){
		Item item = cat.GetComponent<Item>();
		int rowIndex = item.rowIndex;
		int columnIndex = item.columnIndex;


		if(columnIndex == 0 || columnIndex ==columnNum-1 ||
		   rowIndex == 0 || rowIndex == rowNum -1){
			return true;
		}

		return false;
	}

	bool MoveAble(Vector2 v){
		Item item = GetPot((int)v.y, (int)v.x);
		if(item == null){
			return false;
		}

		return item.moveable;
	}

	ArrayList FindSteps(){
		Item item = cat.GetComponent<Item>();
		int rowIndex = item.rowIndex;
		int columnIndex = item.columnIndex;

		ArrayList steps = new ArrayList();
		Vector2 v = new Vector2();
		//left
		v.y = rowIndex;
		v.x = columnIndex -1;
		if(MoveAble(v)) steps.Add(v);

		//right
		v.y = rowIndex;
		v.x = columnIndex +1;
		if(MoveAble(v)) steps.Add(v);

		//top
		v.y = rowIndex+1;
		v.x = columnIndex ;
		if(MoveAble(v)) steps.Add(v);


		//down
		v.y = rowIndex -1;
		v.x = columnIndex ;
		if(MoveAble(v)) steps.Add(v);


		//奇数行：top-left 偶数行 :top-right
		v.y = rowIndex +1;
		if(rowIndex%2==1){
			v.x = columnIndex -1;
		}else if(rowIndex %2==0){
			v.x = columnIndex+1;
		}
		if(MoveAble(v)) steps.Add(v);

		//偶数行:bottom-left,奇数行:bottom-right
		v.y = rowIndex -1;
		if(rowIndex%2==1){
			v.x = columnIndex -1;
		}else if(rowIndex %2==0){
			v.x = columnIndex+1;
		}
		if(MoveAble(v)) steps.Add(v);

		return steps;

	}
	Item createPot(GameObject pot, int rowIndex, int columnIndex){
		GameObject obj = Instantiate(pot) as GameObject;
		Item item = obj.GetComponent<Item>();
		item.game = this;
		item.transform.parent = this.transform;
		item.UpdatePos(rowIndex, columnIndex);

		return item;
	}

	public void StartGame(){
		this.isStarted = true;
		this.gameOver = false;

		startScreen.SetActive(false);
		victory.SetActive(false);
		failed.SetActive(false);
		replay.SetActive(false);

		cat.SetActive(true);

		//reset pot1 moveable
		for(int rowIndex=0; rowIndex < rowNum; rowIndex++){
			ArrayList tmp = new ArrayList();
			for(int columnIndex=0; columnIndex< columnNum; columnIndex++){
				Item item = GetPot(rowIndex, columnIndex);
				item.moveable = true;
			}
			potArray.Add(tmp);
		}

		//remove all pot2
		for (int i=0; i< pot2Array.Count;i++){
			Item pot2Item = pot2Array[i] as Item;

			Destroy(pot2Item.gameObject);
		}
		pot2Array.Clear();

		MoveCat(Random.Range(3, rowNum-3),Random.Range(3, columnNum-3));
	}
}
